class Solution {
public:
    int robR(vector<int>& nums,int l,int r){
        vector<int> dp(nums.size(),0);
        dp[l] = nums[l];
        dp[l + 1] = max(nums[l],nums[l + 1]);
        for(int i = l + 2;i <= r;i++){
            dp[i] = max(dp[i - 1],dp[i - 2] + nums[i]);
        }
       return dp[r];
    }
    int rob(vector<int>& nums) {
        if(nums.size() == 0) return 0;
        if(nums.size() == 1) return nums[0];
        if(nums.size() == 2) return max(nums[0],nums[1]);
        int res1 = robR(nums,0,nums.size() - 2);
        int res2 = robR(nums,1,nums.size() - 1);
        return max(res1,res2);
    }
};
